Ext.define('App.controller.customer.CustomerViewController', {
	extend : 'Ext.app.ViewController',
	alias : 'controller.customerViewController',
	menu : Ext.widget('menu'),
	control : {
		'customerView customerSearch basesearchform' : {
            beforerender : function(form) {
                var buttons = {
                    'crm/customer/saveCustomer' : function(permit) {
                        var mobile = form.down("[name=mobile]").getValue();
                        if(!mobile){
                            BaseUtil.toast("请填写手机号");
                            return;
                        }

                        var customerName = form.down("[name=customerName]").getValue();
                        if(!customerName){
                            BaseUtil.toast("请填写会员名");
                            return;
                        }
                        BaseUtil.post(permit.url,function(data){
                            BaseUtil.toast(data.msg);
                            Ext.StoreMgr.get('customer.CustomerStore').reload();
                        },form.getValues());
                    },
                    'crm/customer/updateCustomer' : function(permit) {
                        var mobile = form.down("[name=mobile]").getValue();
                        if(!mobile){
                            BaseUtil.toast("请填写手机号");
                            return;
                        }
                        var customerName = form.down("[name=customerName]").getValue();
                        if(!customerName){
                            BaseUtil.toast("请填写会员名");
                            return;
                        }
                        BaseUtil.post(permit.url,function(data){
                            BaseUtil.toast(data.msg);
                            Ext.StoreMgr.get('customer.CustomerStore').reload();
                        },form.getValues());
                    }

                };
                BaseUtil.createPermitBar(form, buttons);
            },

			render : function() {
				BaseUtil.loadStore(Ext.StoreMgr.get('customer.CustomerStore'),Ext.getCmp('customerView').down('customerSearch form').getForm().getFieldValues());
			}
		},
        'customerView customerList':{
            itemclick:function(view, record, item, index, e, eOpts){
                var search  =  Ext.getCmp('customerView').down('customerSearch form');
                search.reset();
                search.loadRecord(record);
            },
            itemcontextmenu : function(view, record, item, index, e) {
                var userName = record.get('userName');
                console.log(userName);
                if(userName !== Constant.ADMIN){
                    var userId = record.get('userId');
                    var id = record.get('id');
                    var currentId = BaseUtil.getData("sys/user/getCurUserId").data;
                    var menuitems=[];
                    if(currentId!=userId){
                        menuitems['crm/customer/grant'] = function(permit) {
                            var win = BaseUtil.createView('customer.CustomerGrantWindow',permit);
                            win.down('[name=userId]').setValue(userId);
                            win.show();
                        }
                      var status = record.get('status'),store = Ext.StoreMgr.get('customer.CustomerStore');
                        menuitems['crm/customer/unbind'] = function(permit) {
                                    BaseUtil.statusConfirm('确定解除<font color=red> '+userName+' </font>会员吗?',permit.url,id,store);
                        }


                        // if (status === StatusEnum.NORMAL.getValue()) {
                        //     menuitems['sys/user/freeze'] = function(permit) {
                        //         BaseUtil.statusConfirm('确认冻结<font color=red> '+userName+' </font>吗?',permit.url,id,store);
                        //     }
                        // }else if (status === StatusEnum.FREEZE.getValue()) {
                        //     menuitems['sys/user/unfreeze'] = function(permit) {
                        //         BaseUtil.statusConfirm('确认解冻<font color=red> '+userName+' </font>吗?',permit.url,id,store);
                        //     }
                        // }
                        // if(status === StatusEnum.DELETE.getValue()){
                        //     menuitems['sys/user/recover'] = function(permit) {
                        //         BaseUtil.statusConfirm('确认恢复<font color=red> '+userName+' </font>吗?',permit.url,id,store);
                        //     }
                        // }else{
                        //     menuitems['sys/user/removeUser'] = function(permit) {
                        //         BaseUtil.statusConfirm('确认删除<font color=red> '+userName+' </font>吗?',permit.url,id,store);
                        //     }
                        // }
                    }
                    menuitems['sys/user/resetPwd'] = function(permit) {
                        Ext.Msg.confirm('提示','确认将用户<font color=green> '+userName+' </font>的密码重置为<font color=red> '+ Constant.USER_RESETPWD +' </font>吗?',function(flag){
                            if(flag==='yes'){
                                BaseUtil.post(permit.url,function(data){
                                    BaseUtil.toast(data.msg);
                                },{'id':id});
                            }
                        });
                    },
                        BaseUtil.createPermitMenu(view, this.menu, e, menuitems);
                }
            },
        },
		'customerView customerSearch [action=search] ' : {
			click : function() {
				BaseUtil.loadStore(Ext.StoreMgr.get('customer.CustomerStore'),Ext.getCmp('customerView').down('customerSearch form').getForm().getFieldValues());
			}
		}
	}
});